package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.query.MemberQueryObject;
import cn.wolfcode.crm.service.IMemberService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.MemberAnalysisInfoJson;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 丧彪
 */
@Controller
@RequestMapping("member")
public class MemberController {
    @Autowired
    private IMemberService memberService;

    @RequestMapping("")
    public String index() {
        return "member/list";
    }

    @RequestMapping("rechargePage")
    public String page() {
        //跳转充值页面
        return "member/rechargePage";
    }


    //@RequiresPermissions(value= {"member:list","会员列表"})
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(MemberQueryObject qo) {
        return memberService.query(qo);
    }

    @RequestMapping("save")
    @ResponseBody
    @RequiresPermissions("member:save")
    @PermissionName("会员新增")
    public JsonResult save(Member entity) {
        JsonResult result = new JsonResult();
        try {
            memberService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("保存操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("member:update")
    @PermissionName("会员更新")
    public JsonResult update(Member entity) {
        JsonResult result = new JsonResult();
        try {
            memberService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("编辑操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    @RequiresPermissions("member:changeState")
    @PermissionName("会员卡挂失恢复")
    public JsonResult changeState(Long id) {
        JsonResult result = new JsonResult();
        try {
            memberService.changeState(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("操作失败" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("changePassword")
    @ResponseBody
    @RequiresPermissions("member:changePassword")
    @PermissionName("修改会员密码")
    public JsonResult changePassword(Long id, String newpassword) {
        JsonResult result = new JsonResult();
        try {
            memberService.changePassword(id, newpassword);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("操作失败" + e.getMessage());
        }
        return result;
    }


    @RequestMapping("changeCardno")
    @ResponseBody
    @RequiresPermissions(value = {"member:changeCardno", "会员补卡"})
    public JsonResult changeCardno(Long id,Long cardno) {
        JsonResult result = new JsonResult();
        try {
            memberService.changeCardno(id,cardno);
        } catch (Exception e) {
            e.printStackTrace();
            result.markMsg("操作失败,请重试!" + e.getMessage());
        }
        return result;
    }



    //会员列表页面四个统计表格数据输出
    @RequestMapping("analysisInfo")
    @ResponseBody
    public MemberAnalysisInfoJson analysisInfo() {
        return memberService.analysisInfo();
    }



}
